from django.shortcuts import render
from .forms import UserAskForm, UserCommentForm
from .models import UserLove, UserComment
from django.http import JsonResponse
from orgs.models import OrgInfo, TeacherInfo
from courses.models import CoursesInfo
from tools.decorators import login_decorator


# Create your views here.


def user_ask(request):
    user_ask_form = UserAskForm(request.POST)
    if user_ask_form.is_valid():
        # 这里.save直接代替了name=xxx   a.save  之类的复杂保存
        user_ask_form.save(commit=True)

        return JsonResponse({
            "status": "ok",
            "msg": "咨询成功"
        })
    else:
        return JsonResponse({
            "status": "fail",
            "msg": "咨询失败"
        })


@login_decorator
def user_love(request):
    loveid = request.GET.get("loveid", "")
    lovetype = request.GET.get("lovetype", "")
    if loveid and lovetype:
        # 根据收藏类型，判断是什么对象，根据收藏id判断收藏的哪个对象
        if int(lovetype) == 1:
            obj = OrgInfo.objects.filter(id=int(loveid))[0]
        elif int(lovetype) == 2:
            obj = CoursesInfo.objects.filter(id=int(loveid))[0]
        elif int(lovetype) == 3:
            obj = TeacherInfo.objects.filter(id=int(loveid))[0]
        # print(loveid, lovetype, request.user)
        if request.user.is_authenticated:
            love = UserLove.objects.filter(love_id=int(loveid), love_type=int(lovetype), love_man=request.user)
            if love:
                # print(love[0].love_statu)
                # print(love)
                # 如果本来已经存在收藏这个东西的记录，那么我们需要判断收藏的状态，如果收藏状态为真，代表之前收藏过，并且现在的页面上应显示的是取消收藏，代表着这次点击是为了取消收藏
                if love[0].love_statu:
                    love[0].love_statu = False
                    love[0].save()
                    obj.love_num -= 1
                    obj.save()
                    return JsonResponse({'status': 'ok', 'msg': '收藏'})
                # 如果收藏状态为假，代表之前收藏过，并且又取消了收藏，并且现在的页面上应显示的是收藏，代表着这次点击是为了收藏
                else:
                    love[0].love_statu = True
                    love[0].save()
                    obj.love_num += 1
                    obj.save()
                    return JsonResponse({'status': 'ok', 'msg': '取消收藏'})

            else:
                a = UserLove()
                a.love_man = request.user
                a.love_id = int(loveid)
                a.love_type = int(lovetype)
                a.love_statu = True
                a.save()
                obj.love_num += 1
                obj.save()
                return JsonResponse({
                    "status": "ok",
                    "msg": "收藏"
                })
        else:
            return JsonResponse({
                "status": "fail",
                "msg": "收藏失败-2"
            })
    else:
        return JsonResponse({
            "status": "fail",
            "msg": "收藏失败-1"
        })


def user_comment(request):
    user_comment_form = UserCommentForm(request.POST)
    if user_comment_form.is_valid():
        course = user_comment_form.cleaned_data["course"]
        content = user_comment_form.cleaned_data["content"]
        a = UserComment()
        a.comment_man = request.user
        a.comment_content = content
        a.comment_course_id = course
        a.save()
        return JsonResponse({"status": "ok", "msg": "评论成功"})
    else:
        return JsonResponse({"status": "fail", "msg": "评论失败"})


def user_deletelove(request):
    loveid = request.GET.get("loveid", "")
    lovetype = request.GET.get("lovetype", "")
    if loveid and lovetype:
        love = UserLove.objects.filter(love_id=int(loveid), love_type=int(lovetype), love_man=request.user,
                                       love_statu=True)
        if love:
            love[0].love_statu = False
            love[0].save()
            return JsonResponse({"status": "ok", "msg": "取消成功"})
        else:
            return JsonResponse({"status": "fail", "msg": "发生错误"})
    else:
        return JsonResponse({"status": "fail", "msg": "取消失败"})
